
from minio.error import ResponseError
from minio import Minio


minioClient = Minio('minio:9000',
                    access_key='minioadmin',
                    secret_key='minioadmin',
                    secure=False)

class Bucket:
    # 创建桶(调用make_bucket来创建一个存储桶) make_bucket函数
    """
    注：创建桶命名限制：小写字母，句点，连字符和数字是
    唯一允许使用的字符（使用大写字母、下划线等命名会报错），长度至少应为3个字符
    """
    def create_bucket(self, name):
        try:
            if minioClient.bucket_exists(bucket_name=name):  # bucket_exists：检查桶是否存在
                return 1
            else:
                minioClient.make_bucket(name)
                return 0
        except ResponseError as err:
            print(err)

    # 列出所有的存储桶 list_buckets函数
    def get_bucket_list(self):
        try:
            buckets = minioClient.list_buckets()
            for bucket in buckets:
                print(dir(bucket))
                # print(bucket.name, bucket.creation_date)  # 获取桶的名称和创建时间
            return buckets
        except ResponseError as err:
            print(err)

    # 删除存储桶
    def get_remove_bucket(self,name):
        try:
            minioClient.remove_bucket(name)
            return 1
        except ResponseError as err:
            return 0

    # 列出存储桶中所有对象  或者使用 list_objects_v2也可
    def get_bucket_files(self,name):
        try:
            objects = minioClient.list_objects(name, prefix=None,
                                               recursive=True)  # prefix用于过滤的对象名称前缀
            for obj in objects:
                print(obj.bucket_name, obj.object_name.encode('utf-8'), obj.last_modified,
                      obj.etag, obj.size, obj.content_type)
        except ResponseError as err:
            print(err)

    # 列出存储桶中未完整上传的对象
    def get_list_incomplete_uploads(self,name):
        try:
            uploads = minioClient.list_incomplete_uploads(name,
                                                          prefix=None,
                                                          recursive=True)
            for obj in uploads:
                print(obj.bucket_name, obj.object_name, obj.upload_id, obj.size)
        except ResponseError as err:
            print(err)

    # 获取存储桶的当前策略
    def bucket_policy(self,name):
        try:
            policy = minioClient.get_bucket_policy(name)
            print(policy)
        except ResponseError as err:
            print(err)

    # 获取存储桶上的通知配置
    def bucket_notification(self,name):
        try:
            # 获取存储桶的通知配置。
            notification = minioClient.get_bucket_notification(name)
            print(notification)
            # 如果存储桶上没有任何通知：
            # notification  == {}
        except ResponseError as err:
            print(err)

    # 给存储桶设置通知配置
    def set_bucket_notification(bucket_name, notification):
        pass

    # 删除存储桶上配置的所有通知
    def remove_all_bucket_notifications(bucket_name):
        try:
            minioClient.remove_all_bucket_notifications(bucket_name)
        except ResponseError as err:
            print(err)

    # 监听存储桶上的通知
    def listen_bucket_notification(bucket_name, prefix, suffix, events):
        pass


    def get_bucket_size(self,bucket_name):

        # 获取存储桶中所有对象信息的列表
        objects = minioClient.list_objects(bucket_name, recursive=True)

        # 遍历所有对象并计算存储桶的总大小
        total_size = 0
        for obj in objects:
            total_size += obj.size

        # 将字节大小转换为MB并格式化输出
        total_size_mb = total_size / 1024 / 1024
        return round(total_size_mb,2)
        # print(f"Total size of {bucket_name} is {round(total_size_mb,2)} MB")
